package com.quality.mybatis.utils;


import com.quality.mybatis.pojo.Project;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;

public class getInfoUtil {

    private getInfoUtil() {
    }

    public static Project getInfo(Project project, String url, String username, String password) {

        try {
            // 新建一个Project对象
            String credentials = username + ":" + password;
            String base64Credentials = java.util.Base64.getEncoder().encodeToString(credentials.getBytes());

            CloseableHttpClient httpClient = HttpClients.createDefault();

            HttpGet request = new HttpGet(url);
            request.setHeader("Authorization", "Basic " + base64Credentials);

            HttpResponse response = httpClient.execute(request);

            // 检查响应状态码
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                String responseBody = EntityUtils.toString(response.getEntity());

                // 使用 Jackson 解析 JSON
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode rootNode = objectMapper.readTree(responseBody);

                // 获取指定键的值
                JsonNode valueNode = rootNode.path("facets");

                if (valueNode.isArray()) {
                    for (JsonNode facetNode : valueNode) {
                        // 获取每个 facet 对象中的 property 和 values 键
                        String property = facetNode.path("property").asText();
                        // 严重程度
                        if (property.equals("severities")) {
                            // 获取里面的values
                            JsonNode valuesNode = facetNode.path("values");
                            if (valuesNode.isArray()) {
                                for (JsonNode value : valuesNode) {
                                    String val = value.path("val").asText();
                                    int count = value.path("count").asInt();
                                    if (val.equals("MAJOR")) {
                                        project.setMajor(project.getMajor() + count);
                                    } else if (val.equals("MINOR")) {
                                        project.setMinor(project.getMinor() + count);
                                    } else if (val.equals("CRITICAL")) {
                                        project.setCritical(project.getCritical() + count);
                                    } else if (val.equals("BLOCKER")) {
                                        project.setBlocker(project.getBlocker() + count);
                                    } else if (val.equals("INFO")) {
                                        project.setInfo(project.getInfo() + count);
                                    }
                                }
                            }
                        } else if (property.equals("types")) {
                            // types里面的属性

                            JsonNode valuesNode = facetNode.path("values");
                            if (valuesNode.isArray()) {
                                if (valuesNode.isArray()) {
                                    for (JsonNode value : valuesNode) {
                                        String val = value.path("val").asText();
                                        int count = value.path("count").asInt();
                                        if (val.equals("CODE_SMELL")) {
                                            project.setCodeSmell(project.getCodeSmell() + count);
                                        } else if (val.equals("BUG")) {
                                            project.setBug(project.getBug() + count);
                                        } else if (val.equals("VULNERABILITY")) {
                                            project.setVulnerability(project.getVulnerability() + count);
                                        }
                                    }
                                    return project;
                                }
                            }
                        } else {
                            System.out.println("不可用字段");
                        }
                    }
                }
            } else {
                System.out.println("HTTP 请求失败：" + statusCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
